clear
clear mata
clear matrix

cd ..																			/* Goes back to parent folder */

                                                                   		

capture log close
log using "log\11_Censuses.log", replace

mkdir /tmp                                                                       /* creates a temporary folder to store temp files */



*       Patriotism!

*       FIRST VERSION  June       7, 2022
*       THIS VERSION   June       7, 2022
*       LAST RUN       June       7, 2022

*       LAST REVISOR		AT

*       Log of revisions:

*       This prepares 1910, 1930, 1940 censuses data

********************************************************************************
****                      PLAN OF THE PROCEDURE                             ****
****  1. 1910                                               				****
****  2. 1930       														****
****  3. 1930: Social Security Act       									****
****  4. 1940   															****
****  5. Erase junk                                           			    ****
****                                                                        ****
********************************************************************************

********************************************************************************
****  1. 1910                                               				****
********************************************************************************
do "code-build/01_Shopping_List.do"

********************************************************************************
****    a. Import data                                                      ****
********************************************************************************
clear
quietly infix               ///
  double  hhwt       1-10   ///
  byte    stateicp   11-12  ///
  int     countyicp  13-16  ///
  byte    urban      17-17  ///
  byte    farm       18-18  ///
  byte    ownershp   19-19  ///
  byte    ownershpd  20-21  ///
  double  perwt      22-31  ///
  byte    relate     32-33  ///
  int     related    34-37  ///
  byte    sex        38-38  ///
  int     age        39-41  ///
  byte    marst      42-42  ///
  byte    race       43-43  ///
  int     raced      44-46  ///
  int     bpl        47-49  ///
  long    bpld       50-54  ///
  int     mbpl       55-57  ///
  long    mbpld      58-62  ///
  int     fbpl       63-65  ///
  long    fbpld      66-70  ///
  byte    citizen    71-71  ///
  byte    empstat    72-72  ///
  byte    empstatd   73-74  ///
  using "rawdata/IPUMS/Census1910.dat"											/* This data in available on IPUMS */
	 
********************************************************************************
****    b. Save data on tmp                                                 ****
********************************************************************************
gen  n = _n
save "tmp/C10", replace

********************************************************************************
****    c. Create variables                                                 ****
****        i. Split data into 9 batches                                    ****
********************************************************************************
forvalues batch = 0(1)9 {
	local start =  `batch'      * 10000000
	local end   = (`batch' + 1) * 10000000
	use  "tmp/C10" if n >= `start' & n < `end', replace
	drop n
	 
********************************************************************************
****        ii. Preliminary cleaning (from IPUMS code)                       ****
********************************************************************************
	replace hhwt      = hhwt      / 100
	replace perwt     = perwt     / 100

	compress hhwt perwt

********************************************************************************
****        iii. Create variables                                             ****
********************************************************************************

	gen  byte C10RURAL    = perwt if urban    ==   1
	gen  byte C10URBAN    = perwt if urban    ==   2
	drop urban

	gen  byte C10NFARM    = perwt if farm     ==   1
	gen  byte C10FARM     = perwt if farm     ==   2
	drop farm

	gen  byte C10ITA      = perwt if bpl      == 434 | mbpl == 434 | fbpl == 434 /* Italy   */
	gen  byte C10DEU      = perwt if bpl      == 453 | mbpl == 453 | fbpl == 453 /* Germany */
	gen  byte C10JAP      = perwt if bpl      == 501 | mbpl == 501 | fbpl == 501 /* Japan   */

	drop bpl fbpl mbpl

	gen  byte C10BLACK    = perwt if race     ==   2
	drop race

	gen  byte C10EMP      = perwt if empstat  ==   1
	gen  byte C10UNEMP    = perwt if empstat  ==   2
	drop empstat

	gen  byte C10MEN      = perwt if sex      ==   1
	drop sex

	gen  byte C10POP      = perwt
	drop perwt

********************************************************************************
****        iv. Collapse at the county level                                 ****
********************************************************************************
	collapse (sum) C10POP C10MEN C10RURAL C10URBAN C10FARM C10NFARM C10ITA C10DEU C10JAP C10BLACK C10EMP C10UNEMP, by(stateicp county)

	save "tmp/C10-pt`batch'", replace
} /* forvalues batch */

********************************************************************************
****    d. Append the 13 batches & collapse again                           ****
********************************************************************************
clear
for BATCH in num 0/9 : append using "tmp/C10-ptBATCH"

collapse (sum) C10POP C10MEN C10RURAL C10URBAN C10FARM C10NFARM C10ITA C10DEU C10JAP C10BLACK C10EMP C10UNEMP, by(stateicp county)

ren stateicp stateicpsr
ren county   countyicpsr

replace countyicpsr =  270 if stateicpsr == 65 & countyicpsr ==  250 /* Pershing, Nevada.  */
replace countyicpsr =  250 if stateicpsr == 65 & countyicpsr ==  510 /* Carson City, Nevada is really Ormsby, Nevada.  */
replace countyicpsr =  610 if stateicpsr == 72 & countyicpsr ==  605 /* Union, Oregon.  */

ren      countyicp countyicpsr10
merge 1:m stateicpsr countyicpsr10 using "data/Bridge-countyicpsr1910-countynd", nogen    /* Not matched are Alaska (4), Hawaii (4) and Virgina (1) from master. Not matched are SD (3), AZ (1), MT (1) & WY (4) from using. */

merge m:1 stateicpsr countynd using data/CountiesND_WGS84, nogen keep(mat) keepusing(areand countyname state areand)

for VAR in var C10POP C10MEN C10RURAL C10URBAN C10FARM C10NFARM C10ITA C10DEU C10JAP C10BLACK C10EMP C10UNEMP : replace VAR = VAR * weight10
collapse (sum) C10POP C10MEN C10RURAL C10URBAN C10FARM C10NFARM C10ITA C10DEU C10JAP C10BLACK C10EMP C10UNEMP, by(stateicp state countynd countyname areand)

********************************************************************************
****    e. Correct Greenlee, AZ                                             ****
********************************************************************************

 gen graham_greenlee      = 1 if state == "Arizona" & (countyname == "Greenlee" | countyname == "Graham")
egen area_graham_greenlee = sum(areand) if graham_greenlee != ., by(graham_greenlee)
 gen sh_area              = areand / area_graham_greenlee
 
  for VAR in var C10POP C10MEN C10RURAL C10URBAN C10FARM C10NFARM C10ITA C10DEU C10JAP C10BLACK C10EMP C10UNEMP : egen    VARt = max(VAR)           if graham_greenlee != ., by(graham_greenlee)
 for VAR in var C10POP C10MEN C10RURAL C10URBAN C10FARM C10NFARM C10ITA C10DEU C10JAP C10BLACK C10EMP C10UNEMP  : replace VAR  =     VARt * sh_area if graham_greenlee != .
 
********************************************************************************
****    f. Create variables                                                 ****
********************************************************************************
gen c10urban    = C10URBAN    / (C10RURAL    + C10URBAN  )
gen c10urban1   = c10urban    >           0 			   	if c10urban != .
gen c10men      = C10MEN      /  C10POP
gen c10ita      = C10ITA      /  C10POP
gen c10deu      = C10DEU      /  C10POP
gen c10jap      = C10JAP      /  C10POP
gen c10black    = C10BLACK    /  C10POP
gen c10unemp    = C10UNEMP    / (C10EMP      + C10UNEMP  )


********************************************************************************
****    g. Label & save                                                     ****
********************************************************************************
keeporder stateicpsr countynd c10unemp c10urban1 c10men c10black c10jap c10deu c10ita C10POP

lab var C10POP         "Population in 1910 (IPUMS)"
lab var c10men         "Share of men in 1910 (IPUMS)"
lab var c10urban1      "=1 if county is at least partly urban in 1910 (IPUMS)"
lab var c10ita         "Share of people with Italian ancestry in 1910 (IPUMS)"
lab var c10deu         "Share of people with German ancestry in 1910 (IPUMS)"
lab var c10jap         "Share of people with Japanese ancestry in 1910 (IPUMS)"
lab var c10black       "Share of black people in 1910 (IPUMS)"
lab var c10unemp       "Share of unemployed in 1910 (IPUMS)"

save "data/C10", replace

********************************************************************************
****  2. 1930                                               				****
********************************************************************************
********************************************************************************
****                          I. Full count                                 ****
****    a. Import data                                                      ****
********************************************************************************
clear
quietly infix                 ///
  double  hhwt       15-24    ///
  byte    stateicp   25-26    ///
  int     county     29-32    ///
  byte    urban      33-33    ///
  byte    farm       35-35    ///
  byte    ownershp   36-36    ///
  byte    radio30    39-39    ///
  double  perwt      44-53    ///
  byte    relate     54-55    ///
  byte    sex        60-60    ///
  int     age        61-63    ///
  byte    marst      66-66    ///
  byte    race       71-71    ///
  int     bpl        75-77    ///
  int     mbpl       83-85    ///
  int     fbpl       91-93    ///
  byte    citizen    99-99    ///
  byte    empstat    112-112  ///
  byte    vet1930    116-116  ///
	    using "rawdata/IPUMS/Census1930.dat"									/* This data in available on IPUMS */
	 
********************************************************************************
****    b. Save data on tmp                                                 ****
********************************************************************************
gen  n = _n
save "tmp/C30", replace

********************************************************************************
****    c. Create variables                                                 ****
****        i. Split data into 13 batches                                   ****
********************************************************************************
forvalues batch = 0(1)12 {
	local start =  `batch'      * 10000000
	local end   = (`batch' + 1) * 10000000
	use  tmp/C30 if n >= `start' & n < `end', replace
	drop n
	 
********************************************************************************
****        ii. Preliminary cleaning (from IPUMS code)                       ****
********************************************************************************
	replace hhwt      = hhwt      / 100
	replace perwt     = perwt     / 100

	compress hhwt perwt

********************************************************************************
****        iii. Create variables                                             ****
********************************************************************************

	gen  byte C30HOMEOWN  = perwt if ownershp ==   1
	gen  byte C30HOMERENT = perwt if ownershp ==   2
	drop ownershp
	
	gen  byte C30RURAL    = perwt if urban    ==   1
	gen  byte C30URBAN    = perwt if urban    ==   2
	drop urban

	gen  byte C30NFARM    = perwt if farm     ==   1
	gen  byte C30FARM     = perwt if farm     ==   2
	drop farm

	gen  byte C30ITA      = perwt if bpl      == 434 | mbpl == 434 | fbpl == 434 /* Italy   */
	gen  byte C30DEU      = perwt if bpl      == 453 | mbpl == 453 | fbpl == 453 /* Germany */
	gen  byte C30JAP      = perwt if bpl      == 501 | mbpl == 501 | fbpl == 501 /* Japan   */
	drop bpl fbpl mbpl

	gen  byte C30BLACK    = perwt if race     ==   2
	drop race

	gen  byte C30EMP      = perwt if empstat  ==   1
	gen  byte C30UNEMP    = perwt if empstat  ==   2
	drop empstat

	gen  byte C30VET      = perwt if inlist(vet1930,1,2,3,4,5) & sex == 1 & age >= 30
	gen  byte C30NVET     = perwt if vet1930  ==    0          & sex == 1 & age >= 30
	drop vet1930

	gen  byte C30MEN      = perwt if sex      ==   1
	drop sex

	gen  byte C30POP      = perwt
	drop perwt

********************************************************************************
****        iv. Collapse at the county level                                 ****
********************************************************************************
	collapse (sum) C30RURAL C30URBAN C30FARM C30NFARM C30HOMEOWN C30HOMERENT C30POP C30MEN C30ITA C30DEU C30JAP C30BLACK C30EMP C30UNEMP C30VET C30NVET, by(stateicp county)

	save tmp/C30-pt`batch', replace
} /* forvalues batch */

********************************************************************************
****    d. Append the 13 batches & collapse again                           ****
********************************************************************************
clear
for BATCH in num 0/12: append using "tmp/C30-ptBATCH"

collapse (sum) C30RURAL C30URBAN C30FARM C30NFARM C30HOMEOWN C30HOMERENT C30POP C30MEN C30ITA C30DEU C30JAP C30BLACK C30EMP C30UNEMP C30VET C30NVET, by(stateicp county)

save "tmp/C30full", replace

********************************************************************************
****                          II. 5% sample                                  ****
****    a. Import data                                                      ****
********************************************************************************
clear
quietly infix                 ///
  double  hhwt       15-24    ///
  byte    stateicp   25-26    ///
  int     county     29-32    ///
  byte    urban      33-33    ///
  byte    farm       35-35    ///
  byte    ownershp   36-36    ///
  byte    radio30    39-39    ///
  double  perwt      44-53    ///
  byte    relate     54-55    ///
  byte    sex        60-60    ///
  int     age        61-63    ///
  byte    marst      66-66    ///
  byte    race       71-71    ///
  int     bpl        75-77    ///
  int     mbpl       83-85    ///
  int     fbpl       91-93    ///
  byte    citizen    99-99    ///
  byte    empstat    112-112  ///
  byte    vet1930    116-116  ///
	 using "rawdata/IPUMS/Census1930_5p.dat"									/* This data in available on IPUMS */
	 
********************************************************************************
****    b. Keep only Kenedy (TX)                                            ****
********************************************************************************
keep if stateicp == 49 & county == 2610

********************************************************************************
****    c. Preliminary cleaning (from IPUMS code)                           ****
********************************************************************************
replace hhwt      = hhwt      / 100
replace perwt     = perwt     / 100

compress hhwt perwt

********************************************************************************
****    d. Create variables                                                 ****
********************************************************************************																			

gen  byte C30HOMEOWN  = perwt if ownershp ==   1
gen  byte C30HOMERENT = perwt if ownershp ==   2
drop ownershp

gen  byte C30RURAL    = perwt if urban    ==   1
gen  byte C30URBAN    = perwt if urban    ==   2
drop urban

gen  byte C30NFARM    = perwt if farm     ==   1
gen  byte C30FARM     = perwt if farm     ==   2
drop farm

gen  byte C30ITA      = perwt if bpl      == 434 | mbpl == 434 | fbpl == 434 /* Italy   */
gen  byte C30DEU      = perwt if bpl      == 453 | mbpl == 453 | fbpl == 453 /* Germany */
gen  byte C30JAP      = perwt if bpl      == 501 | mbpl == 501 | fbpl == 501 /* Japan   */
drop bpl fbpl mbpl

gen  byte C30BLACK    = perwt if race     ==   2
drop race

gen  byte C30EMP      = perwt if empstat  ==   1
gen  byte C30UNEMP    = perwt if empstat  ==   2
drop empstat

gen  byte C30VET      = perwt if inlist(vet1930,1,2,3,4,5) & sex == 1 & age >= 30
gen  byte C30NVET     = perwt if vet1930  ==    0          & sex == 1 & age >= 30
drop vet1930

gen  byte C30MEN      = perwt if sex      ==   1
drop sex

gen  byte C30POP      = perwt
drop perwt

********************************************************************************
****    e. Collapse at the county level                                     ****
********************************************************************************
collapse (sum) C30RURAL C30URBAN C30FARM C30NFARM C30HOMEOWN C30HOMERENT C30POP C30MEN C30ITA C30DEU C30JAP C30BLACK C30EMP C30UNEMP C30VET C30NVET, by(stateicp county)

save "tmp/C30Kenedy", replace

********************************************************************************
****                          C. Append the 2                               ****
****    a. Append the 2 files                                               ****
********************************************************************************
use          "tmp/C30full"  , replace
append using "tmp/C30Kenedy"

ren stateicp stateicpsr
ren county   countyicpsr

replace countyicpsr =  270 if stateicpsr == 65 & countyicpsr ==  250 /* Pershing, Nevada.  */
replace countyicpsr =  250 if stateicpsr == 65 & countyicpsr ==  510 /* Carson City, Nevada is really Ormsby, Nevada.  */
replace countyicpsr =  610 if stateicpsr == 72 & countyicpsr ==  605 /* Union, Oregon.  */

merge 1:1 stateicpsr countyicpsr using "rawdata\maps\Bridge", nogen keep(mat)    /* Not matched are Yellowstone park in Montana */

collapse (sum) C30RURAL C30URBAN C30FARM C30NFARM C30HOMEOWN C30HOMERENT C30POP C30MEN C30ITA C30DEU C30JAP C30BLACK C30EMP C30UNEMP C30VET C30NVET, by(stateicp countynd)

********************************************************************************
****    b. Create shares                                                    ****
********************************************************************************
gen c30farm     = C30FARM     / (C30NFARM    + C30FARM   )
gen c30homeown  = C30HOMEOWN  / (C30HOMERENT + C30HOMEOWN)
gen c30urban    = C30URBAN    / (C30RURAL    + C30URBAN  )
gen c30urban1   = c30urban    >           0 if c30urban != .

gen c30men      = C30MEN      /  C30POP
gen c30ita      = C30ITA      /  C30POP
gen c30deu      = C30DEU      /  C30POP
gen c30jap      = C30JAP      /  C30POP
gen c30black    = C30BLACK    /  C30POP

gen c30unemp    = C30UNEMP    / (C30EMP      + C30UNEMP  )
gen c30vet      = C30VET      / (C30NVET     + C30VET    )

********************************************************************************
****    c. Label & save                                                     ****
********************************************************************************
keeporder stateicpsr countynd C30HOMEOWN C30POP C30EMP C30UNEMP c30urban1 c30farm c30men c30black c30jap c30deu c30ita c30vet c30unemp

lab var C30POP         "Population in 1930 (IPUMS)"
lab var C30HOMEOWN     "Number of households owning the house in 1930 (IPUMS)"
lab var C30EMP         "Number of employed people in 1930 (IPUMS)"
lab var C30UNEMP       "Number of unemployed in 1930 (IPUMS)"
lab var c30men         "Share of men in 1930 (IPUMS)"
lab var c30urban1      "=1 if county is at least partly urban in 1930 (IPUMS)"
lab var c30ita         "Share of people with Italian ancestry in 1930 (IPUMS)"
lab var c30deu         "Share of people with German ancestry in 1930 (IPUMS)"
lab var c30jap         "Share of people with Japanese ancestry in 1930 (IPUMS)"
lab var c30black       "Share of black people in 1930 (IPUMS)"
lab var c30unemp       "Share of unemployed in 1930 (IPUMS)"
lab var c30vet         "Share of veterans among men 30 or older in 1930 (IPUMS)"
lab var c30farm        "Share of households living on a farm in 1930 (IPUMS)"

save "data/C30", replace

********************************************************************************
****  2. 1930: Social Security Act                                          ****
********************************************************************************
********************************************************************************
****                          I. Full count                                 ****
****    a. Import data                                                      ****
********************************************************************************
clear
quietly infix                   ///
  double  serial       11-18    ///
  byte    stateicp     29-30    ///
  int     countyicp    31-34    ///
  int     pernum       36-39    ///
  double  perwt        40-49    ///
  byte    relate       50-51    ///
  byte    sex          56-56    ///
  int     age          57-59    ///
  byte    marst        60-60    ///
  byte    empstat      61-61    ///
  byte    classwkrd    66-67    ///
  int     ind1950      68-70    ///
	 using "rawdata/IPUMS/Census1930.dat"										/* This data in available on IPUMS */
	 
********************************************************************************
****    2. Create variables                                                 ****
********************************************************************************
	 
********************************************************************************
****        b. Preliminary cleaning (from IPUMS code)                       ****
********************************************************************************
replace perwt     = perwt     / 100

compress perwt

********************************************************************************
****        c. Create variables                                             ****
****            i. Old age                                                  ****
********************************************************************************
gen C30BENSSOAl = perwt if empstat == 1 &                                 ///  Must be employed
                  (classwkrd == 20 | classwkrd == 21 | classwkrd == 22) & ///  working for wage (no self employed), not in non-profit/government 
                   ind1950 != 105 & ind1950 != 116 & ind1950 != 126 &     ///  Not in agriculture
				   ind1950 != 506 & ind1950 != 516                  &     ///  Not in railways
                   ind1950 <  900                                         /*   Not in government, domestic services, other non-productive industries */
																				
gen C30BENSSOAs = perwt if C30BENSSOAl == 1 & age >= 55
																				
********************************************************************************
****           ii. Dependent children                                       ****
********************************************************************************
 gen C30DEPCHI  = age <= 11 & (relate == 3 | relate == 4)                       /* Less than 11 y.o. in 1930 => less than 16 years in 1935; relate = 3,4 for children (natural or in law) */
egen C30NDEPCHI = sum(C30DEPCHI), by(serial)
 gen C30BENSSDC = perwt * C30NDEPCHI if sex == 2 & relate == 1 & (marst == 2 | marst == 3 | marst == 4 | marst == 5) & C30NDEPCHI > 0 & C30NDEPCHI != .
                                                                                /* Women, head of household, with no husband and at least one dependent child */
                                                                                /* Gets support equal to the number of dependent children */ 

********************************************************************************
****        d. Collapse at the county level                                 ****
********************************************************************************
collapse (sum) C30BENSSOAl C30BENSSOAs C30BENSSDC, by(stateicp county)

save "tmp/C30fullSS", replace

********************************************************************************
****                          B. 5% sample                                  ****
****    1. Import data                                                      ****
********************************************************************************
clear
quietly infix               ///
  double  serial     11-18  ///
  byte    stateicp   42-43  ///
  int     countyicp  44-47  ///
  int     pernum     61-64  ///
  double  perwt      65-74  ///
  byte    relate     75-76  ///
  byte    sex        81-81  ///
  int     age        82-84  ///
  byte    marst      85-85  ///
  byte    empstat    86-86  ///
  byte    classwkrd  91-92  ///
  int     ind1950    93-95  ///
	 using "rawdata/IPUMS/Census1930_5p.dat"									/* This data in available on IPUMS */
	 
********************************************************************************
****    2. Keep only Kenedy (TX)                                            ****
********************************************************************************
keep if stateicp == 49 & county == 2610

********************************************************************************
****    3. Preliminary cleaning (from IPUMS code)                           ****
********************************************************************************
replace perwt     = perwt     / 100

compress perwt

********************************************************************************
****    4. Create variables                                                 ****
****            i. Old age                                                  ****
********************************************************************************
gen C30BENSSOAl = perwt if empstat == 1 &                                 ///  Must be employed
                  (classwkrd == 20 | classwkrd == 21 | classwkrd == 22) & ///  working for wage (no self employed), not in non-profit/government 
                   ind1950 != 105 & ind1950 != 116 & ind1950 != 126 &     ///  Not in agriculture
				   ind1950 != 506 & ind1950 != 516                  &     ///  Not in railways
                   ind1950 <  900                                         /*   Not in government, domestic services, other non-productive industries */
																				
gen C30BENSSOAs = perwt if C30BENSSOAl == 1 & age >= 55
																				
********************************************************************************
****           ii. Dependent children                                       ****
********************************************************************************
 gen C30DEPCHI  = age <= 11 & (relate == 3 | relate == 4)                       /* Less than 11 y.o. in 1930 => less than 16 years in 1935; relate = 3,4 for children (natural or in law) */
egen C30NDEPCHI = sum(C30DEPCHI), by(serial)
 gen C30BENSSDC = perwt * C30NDEPCHI if sex == 2 & relate == 1 & (marst == 2 | marst == 3 | marst == 4 | marst == 5) & C30NDEPCHI > 0 & C30NDEPCHI != .
                                                                                /* Women, head of household, with no husband and at least one dependent child */
                                                                                /* Gets support equal to the number of dependent children */ 
																				
********************************************************************************
****    5. Collapse at the county level                                     ****
********************************************************************************
collapse (sum) C30BENSSOAl C30BENSSOAs C30BENSSDC, by(stateicp county)

save "tmp/C30KenedySS", replace

********************************************************************************
****                          C. Append the 2                               ****
****    1. Append the 2 files                                               ****
********************************************************************************
use          "tmp/C30fullSS"  , replace
append using "tmp/C30KenedySS"

collapse (sum) C30BENSSOAs C30BENSSDC, by(stateicp county)

********************************************************************************
****    2. Collapse at the level of New Deal counties                       ****
********************************************************************************
ren stateicp stateicpsr
ren county   countyicpsr

replace countyicpsr =  270 if stateicpsr == 65 & countyicpsr ==  250 /* Pershing, Nevada.  */
replace countyicpsr =  250 if stateicpsr == 65 & countyicpsr ==  510 /* Carson City, Nevada is really Ormsby, Nevada.  */
replace countyicpsr =  610 if stateicpsr == 72 & countyicpsr ==  605 /* Union, Oregon.  */

merge 1:1 stateicpsr countyicpsr using "rawdata/maps/Bridge", nogen keep(mat)    /* Not matched are Yellowstone park in Montana */

collapse (sum) C30BENSSOAs C30BENSSDC, by(stateicpsr countynd)

lab var C30BENSSOAs  "Number of men older than 55 yo eligible to SS Old Age cov. (IPUMS)"
lab var C30BENSSDC   "Number of dependent children eligible to SS Dep. Chi. cov. (IPUMS)"

save  "data/C30BENSS" , replace

********************************************************************************
****  4. 1940                                                				****
********************************************************************************

********************************************************************************
****    a. Import data                                                      ****
********************************************************************************
clear
quietly infix                 ///
  double  hhwt       15-24    ///
  byte    stateicp   25-26    ///
  int     county     29-32    ///
  byte    urban      33-33    ///
  byte    farm       35-35    ///
  byte    ownershp   36-36    ///
  double  perwt      43-52    ///
  double  slwt       53-62    ///
  byte    relate     63-64    ///
  byte    sex        69-69    ///
  int     age        70-72    ///
  byte    marst      75-75    ///
  byte    race       78-78    ///
  int     bpl        82-84    ///
  int     mbpl       90-92    ///
  int     fbpl       98-100   ///
  byte    citizen    106-106  ///
  byte    educ       118-119  ///
  byte    empstat    123-123  ///
  byte    empstatd   124-125  ///
  int     ind        131-134  ///
  byte    classwkr   135-135  ///
  byte    classwkrd  136-137  ///
  long    incwage    145-150  ///
  byte    vetstat    158-158  ///
  byte    ssenroll   198-198  ///
	 using "rawdata/IPUMS/Census1940.dat"				    					/* This data in available on IPUMS */

********************************************************************************
****    b. Save data on tmp                                                 ****
********************************************************************************
gen  n = _n
save "tmp/C40", replace

********************************************************************************
****    c. Create variables                                                 ****
****        i. Split data into 14 batches                                   ****
********************************************************************************
forvalues batch = 0(1)13 {
	local start =  `batch'      * 10000000
	local end   = (`batch' + 1) * 10000000
	use  "tmp/C40" if n >= `start' & n < `end', replace
	drop n
	
********************************************************************************
****        ii. Preliminary cleaning (from IPUMS code)                       ****
********************************************************************************
	replace hhwt      = hhwt      / 100
	replace perwt     = perwt     / 100

	compress hhwt perwt slwt

********************************************************************************
****        iii. Create variables                                             ****
********************************************************************************

	gen  byte C40EMP      = perwt   if empstat   ==   1
	gen  byte C40UNEMP    = perwt   if empstat   ==   2
	drop empstat

	gen  long C40WAGE     = perwt * incwage if (classwkrd ==  22 | classwkrd == 24) & incwage <= 5000
	gen  byte C40WAGEEARN = perwt           if (classwkrd ==  22 | classwkrd == 24) & incwage <= 5000
	drop classwkrd incwage

	gen  byte C40POP      = perwt
	drop perwt

********************************************************************************
****        iv. Collapse at the county level                                 ****
********************************************************************************
	collapse (sum) C40POP C40WAGE C40WAGEEARN C40EMP C40UNEMP, by(stateicp county)

	save "tmp/C40-pt`batch'", replace
} /* forvalues batch */

********************************************************************************
****    d. Append the 14 batches & collapse again                           ****
********************************************************************************
clear
for BATCH in num 0/13 : append using "tmp/C40-ptBATCH"

collapse (sum) C40POP C40WAGE C40WAGEEARN C40EMP C40UNEMP, by(stateicp county)

ren stateicp stateicpsr
ren county   countyicpsr40

********************************************************************************
****    e. Collapse for ASN dataset				                            ****
********************************************************************************

preserve
gen c40wage     = C40WAGE     /  C40WAGEEARN
gen c40unemp    = C40UNEMP    / (C40EMP      + C40UNEMP  )
keep stateicpsr countyicpsr C40POP c40unemp c40wage

lab var C40POP         "Population in 1940 (IPUMS)"
lab var c40wage        "Average wage of employees in 1939 (IPUMS)"
lab var c40unemp       "Share of unemployed in 1940 (IPUMS)"

save "data/C40_ASN", replace

restore

********************************************************************************
****    f. Collapse for main dataset				                        ****
********************************************************************************

replace countyicpsr =  270 if stateicpsr == 65 & countyicpsr ==  250 /* Pershing, Nevada.  */
replace countyicpsr =  250 if stateicpsr == 65 & countyicpsr ==  510 /* Carson City, Nevada is really Ormsby, Nevada.  */
replace countyicpsr =  610 if stateicpsr == 72 & countyicpsr ==  605 /* Union, Oregon.  */

merge 1:m stateicpsr countyicpsr40 using "data/Bridge-countyicpsr1940-countynd", nogen keep(mat)    /* Not matched are Alaska (4) & Hawaii (5) from master. */

for VAR in var C40POP C40WAGE C40WAGEEARN C40EMP C40UNEMP: replace VAR = VAR * weight40
collapse (sum) C40POP C40WAGE C40WAGEEARN C40EMP C40UNEMP, by(stateicp countynd)

********************************************************************************
****    g. Create shares                                                    ****
********************************************************************************
 
gen c40unemp    = C40UNEMP    / (C40EMP      + C40UNEMP  )
gen c40wage     = C40WAGE     /  C40WAGEEARN

keeporder stateicp countynd C40POP c40wage c40unemp
********************************************************************************
****    h. Label & save                                                     ****
********************************************************************************
lab var C40POP         "Population in 1940 (IPUMS)"
lab var c40wage        "Average wage of employees in 1939 (IPUMS)"
lab var c40unemp       "Share of unemployed in 1940 (IPUMS)"

save "data/C40", replace

********************************************************************************
****  4. Erase junk                                               		    ****
********************************************************************************

erase "tmp/C10.dta"
for BATCH in num 0/9 : erase "tmp/C10-ptBATCH.dta"
erase "tmp/C30.dta"
for BATCH in num 0/12 : erase "tmp/C30-ptBATCH.dta"
erase "tmp/C30full.dta"
erase "tmp/C30Kenedy.dta"
erase "tmp/C30fullSS.dta"
erase "tmp/C30KenedySS.dta"
erase "tmp/C40.dta"
for BATCH in num 0/13 : erase "tmp/C40-ptBATCH.dta"


rmdir "tmp/"

log close
exit